<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Category_model extends CI_Model
{

    public function getParentCategory($status = true)
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('category');
        if ($status) {
            $this->db->where(array(
                'parent_id' => '0',
                'status' => '1'
            ));
        } else {
            $this->db->where(array(
                'parent_id' => '0'
            ));
        }
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    public function getChildrenCategory($parentId, $status = true)
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('category');
        if ($status) {
            $this->db->where(array(
                'parent_id' => $parentId,
                'status' => '1'
            ));
        } else {
            $this->db->where(array(
                'parent_id' => $parentId
            ));
        }
        $rs = $this->db->get();
        if ($rs->num_rows() > 0) {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        } else {
            $rs->free_result();
            return false;
        }
    }
    
    public function getAllCategory()
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('category');
        $this->db->where('status','1');
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getCategoryById($id)
    {
        $this->db->select('*');
        $this->db->from('category');
        $this->db->where(array(
            'id'    =>  $id
        ));
        $rs = $this->db->get();
        $tmp = $rs->result_array();
        $rs->free_result();
        if (count($tmp) != 0)
            return $tmp[0];
        return false;
    }

    public function categoryExists($category)
    {
        $this->db->select('*');
        $this->db->from('category');
        $this->db->where('name_vn', $category);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }

    public function addCategory($name_vn, $name_en, $parent_id, $href)
    {
        $array = array(
            'parent_id' => $parent_id,
            'name_vn' => $name_vn,
            'name_en' => $name_en,
            'href' => $href,
            'status' => '1'
        );
        return $this->db->insert('category', $array);
    }

    public function updateCategoryStatus($catId, $data = array())
    {
        if (!is_null($data)) {
            if ($childId = $this->getChildrenCategory($catId, false)) {
                foreach ($childId as $c) {
                    $this->db->update('post', $data, array('parent_id'=>$c['id']));
                }
            }
            $this->db->update('category', $data, array(
                'id' => $catId
            ));
            $this->db->update('category', $data, array(
                'parent_id' => $catId
            ));
            $this->db->update('post', $data, array(
                'parent_id' => $catId
            ));
        }
    }
    
    public function updateCategory($catId, $data = array())
    {
        if (!is_null($data)) {
            $this->db->update('category', $data, array(
                'id'    =>  $catId
            ));
            return true;
        }
        return false;
    }

    public function deleteCategory($catId)
    {
        if ($childId = $this->getChildrenCategory($catId, false)) {
            foreach ($childId as $c) {
                $this->db->delete('post', array('parent_id' => $c['id']));
            }
        }
        $this->db->delete('category', array('parent_id' => $catId));
        $this->db->delete('post', array('parent_id' => $catId));
        $this->db->delete('category', array('id' => $catId));
    }
    
    public function getCategoryInfo($catId)
    {
        $return = array();
        $this->db->select('*');
        $this->db->from('category');
        $this->db->where('id', $catId);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

}

?>
